﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Text;

namespace CotizacionPresencialPaquetes.Controllers
{
    public class WordActionResult : ActionResult
    {

        private string _url;

        public string url
        {
            get { return _url; }
        }

        private string _filename;
        public string fileName
        {
            get { return _filename; }
        }



        public WordActionResult(string pUrl, string pFileName)
        {
            _url = pUrl;

            _filename = pFileName;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpContext curContext = HttpContext.Current;
            curContext.Response.Clear();
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + _filename);
            curContext.Response.Charset = "";
            curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            curContext.Response.ContentType = "application/ms-word";

            HttpWebRequest wreq = (HttpWebRequest)HttpWebRequest.Create(_url);
            HttpWebResponse wres = (HttpWebResponse)wreq.GetResponse();
            Stream s = wres.GetResponseStream();
            StreamReader sr = new StreamReader(s, Encoding.ASCII);

            curContext.Response.Write(sr.ReadToEnd());
            curContext.Response.End();
        }
    }
}